import { Component, OnInit } from '@angular/core';
import { CostService } from './cost.service';
import { AppPaginationComponent, PaginationModel } from "../../../../widgets/pagination/pagination.component";
import { zh } from "flatpickr/dist/l10n/zh.js";
import * as _ from 'lodash';
import { environment } from '../../../../../environments/environment';
import { DataService } from '../../../../services/data.service';
@Component({
  selector: 'app-cost',
  templateUrl: './cost.component.html',
  styleUrls: ['./cost.component.scss'],
  providers: [CostService]
})
export class CostComponent implements OnInit {

    // 字典值
    public dictionaryList: any = {
      FUND_TYPE: [],
      S_FEE_TYPE: []
    };

  public postBody: any = {
    pageIndex: 1,
    pageSize: 10,
    nFundNoList: '',
    sBegin: '',
    sEnd: '',
    dateList:'',
    sFeeType: '',
    sFeeTypeList:'',
    sFundType: '',
    sFundCode: ''
  }
  //费用流水分页配置
  public costListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  public ZhInfo: any;
  public costList: any;
  //全选按钮
  public isCheckedAll: any= false;
  constructor(
    public costService: CostService,
    public dataService:DataService
  ) { 
    this.ZhInfo = this.dataService.ZhInfo;
    this.postBody.sFundCode = this.ZhInfo.sFundCode;
    this.postBody.sFundType = this.ZhInfo.sFundType;
  }

  ngOnInit() {
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    this.loadList();
  }
  //列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.costList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.costList, item => {
        item.isChecked = false;
      });
    }
  }
  //列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.costList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.costList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  //查询费用流水信息
  loadList(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.postBody.pageIndex = pageNum ? pageNum : this.costListPageInfo.currentPageNum;
    this.postBody.pageSize = pageSize ? pageSize : this.costListPageInfo.pageSize;

    this.costService.getCostList(this.postBody).subscribe(data => {
      if (data) {
        // console.log("查询成功")
        this.costList = data.list;
        console.log(data);
        that.costListPageInfo.currentPageNum = data.pageNum;
        that.costListPageInfo.totalPages = data.pages;
        that.costListPageInfo.total = data.total;
        that.costListPageInfo.startRow = data.startRow;
        that.costListPageInfo.endRow = data.endRow;
        // this.ctrlCheckAll();
        // console.log(data)
      }
    })
  }
  // 导出
  export() {
    let that = this;
    let checkedItem = [];
    // updated by dujh @20180702 start
    let dateList = [];
    let sFeeTypeList = [];
    //updated by dujh @20180702 end
    _.forEach(that.costList, item => {
      if (item.isChecked) {
        checkedItem.push(item.nFundNo);
        // updated by dujh @20180702 start
        dateList.push(item.tDate);
        sFeeTypeList.push(item.sFeeType);
        // updated by dujh @20180702 end
      }
    });
    // updated by dujh @20180702 start
    // let converItem: any = checkedItem.join(',');
    // updated by dujh @20180702 end
    let getBody = {
      "nFundNoList": checkedItem,
      // updated by dujh @20180702 start
      "dateList": dateList,
      "sFeeTypeList": sFeeTypeList,
      //updated by dujh @20180702 end
      "sBegin": this.postBody.sBegin,
      "sEnd": this.postBody.sEnd,
      "sFundCode": this.ZhInfo.sFundCode,
      "sFundType": this.ZhInfo.sFundType
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    // getBody.prdName = encodeURI(getBody.prdName);
    // updated by dujh @20180702 start
    iframe.attr('src', environment.server + '/pms/v1/productquery/export/exportAccountFee?token=' + sessionStorage.getItem('token') + '&nFundNoList=' + getBody.nFundNoList + '&sBegin=' + getBody.sBegin + '&sEnd=' + getBody.sEnd + '&sFundCode=' + getBody.sFundCode + '&sFundType=' + getBody.sFundType + '&dateList=' + getBody.dateList + '&sFeeTypeList=' + getBody.sFeeTypeList);
    //updated by dujh @20180702 end
    //console.log(environment.server + '/pms/v1/productquery/export/exportAccountFee?token=' + sessionStorage.getItem('token') + '&nFundNoList=' + getBody.nFundNoList + '&sBegin=' + getBody.sBegin + '&sEnd=' + getBody.sEnd + '&sFundCode=' + getBody.sFundCode + '&sFundType=' + getBody.sFundType + '&dateList=' + getBody.dateList + '&sFeeTypeList=' + getBody.sFeeTypeList);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 个数分页
  costpageSizeChange(pageSize: number) {
    if (pageSize !== this.costListPageInfo.pageSize) {
      this.costListPageInfo.pageSize = pageSize;
      this.loadList();
    }
  }
  // 页码分页
  costPageNavigation(currentPageNum: number) {
    this.costListPageInfo.currentPageNum = currentPageNum;
    this.loadList(this.costListPageInfo.currentPageNum, this.costListPageInfo.pageSize);
  }

  showHisListModal() {
    window['$']("#detail-his-list-modal").modal();
  }
}
